
/**
 * @author Doan Tuan Anh
 */

import javax.microedition.midlet.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;

public class GameMidlet extends MIDlet implements CommandListener {
        
      private Command myExitCommand = new Command("Exit", Command.EXIT, 1);
      private Command myGoCommand = new Command("Go", Command.SCREEN, 2);
      private Command myPauseCommand = new Command("Pause", Command.SCREEN, 2);
      private Command myNewCommand = new Command("Play Again", Command.SCREEN, 2);
      private Command myMusicCommand = new Command("Music", Command.SCREEN, 2);

      private MyGameCanvas myCanvas;
      private StartCanvas startCanvas;
      private GameThread myGameThread;
      private MusicPlayer musicPlayer;

      private ClownFishThread myClownFishThread;
      private GoldFishThread myGoldFishThread;
      private PiranhaThread myPiranhaThread;
      
      private boolean myGamePause;
      private boolean myHiddenPause;

      public GameMidlet() {
        try {
          myCanvas = new MyGameCanvas(this);
          myCanvas.addCommand(myExitCommand);
          myCanvas.addCommand(myMusicCommand);
          myCanvas.addCommand(myPauseCommand);
          myCanvas.addCommand(myGoCommand);
          myCanvas.addCommand(myNewCommand);
          myCanvas.setCommandListener(this);
        } catch(Exception e) {

        }
      }
      
      void removeGoCommand()
      {
          myCanvas.removeCommand(myGoCommand);
      }
      
      void addPauseCommand()
      {
          myCanvas.addCommand(myPauseCommand);
      }
      
      void addNewCommand()
      {
          myCanvas.addCommand(myNewCommand);
      }

      void setNewCommand() {
        myCanvas.removeCommand(myPauseCommand);
        myCanvas.removeCommand(myGoCommand);
        myCanvas.addCommand(myNewCommand);
      }

      private void setGoCommand() {
        myCanvas.removeCommand(myPauseCommand);
        myCanvas.removeCommand(myNewCommand);
        myCanvas.addCommand(myGoCommand);
      }

      private void setPauseCommand() {
        myCanvas.removeCommand(myNewCommand);
        myCanvas.removeCommand(myGoCommand);
        myCanvas.addCommand(myPauseCommand);
      }
  
      public void startApp() throws MIDletStateChangeException {
        try {
            
          Display display = Display.getDisplay(this);
          startCanvas = new StartCanvas(this, myCanvas);
          display.setCurrent(startCanvas);
          
          if(myCanvas != null) {
            //myCanvas.start();
            //System.out.println("Here");
            //myCanvas.flushKeys();
            //systemStartThreads();
          }
        } catch(Exception e) {

        }
      }

      public void destroyApp(boolean unconditional) 
          throws MIDletStateChangeException {
        try {
          stopThreads();
          myCanvas = null;
          System.gc(); 
        } catch(Exception e) {

        }
      }

      public void pauseApp() {
        try {
          if(myCanvas != null) {
            setGoCommand();
            systemPauseThreads();
          }
        } catch(Exception e) {

        }
      }

      public void commandAction(Command c, Displayable s) {
        try {
          if(c == myGoCommand) {
              
            myCanvas.removeCommand(myGoCommand);
            myCanvas.addCommand(myPauseCommand);
            myCanvas.addCommand(myNewCommand);
            myCanvas.flushKeys();
            userStartThreads();
                
          } else if(c == myPauseCommand) {
      
            myCanvas.removeCommand(myPauseCommand);
            myCanvas.addCommand(myGoCommand);
            myCanvas.addCommand(myNewCommand);
            userPauseThreads();
          } else if(c == myNewCommand) {
              
            myCanvas.removeCommand(myNewCommand);
            myCanvas.addCommand(myPauseCommand);
            System.gc();

            myCanvas.reset();
            myCanvas.flushKeys();
            myHiddenPause = false;
            myGamePause = false;
            startThreads();
          } else if(c == myMusicCommand) {
            if(musicPlayer != null) {
              musicPlayer.toggle();
              myCanvas.repaint();
              myCanvas.serviceRepaints();
            }
          } else if((c == myExitCommand) || (c == Alert.DISMISS_COMMAND)) {
            try {
              destroyApp(false);
              notifyDestroyed();
            } catch (MIDletStateChangeException ex) {
            }
          } 
        } catch(Exception e) {

        }
      }
  
      private synchronized void userStartThreads() throws Exception {
          
        myGamePause = false;
        if(! myHiddenPause) {
            
          startThreads();
        }
      }

      synchronized void systemStartThreads() throws Exception {
        myHiddenPause = false;
        if(! myGamePause) {
          startThreads();
        }
      }

      private void startThreads() throws Exception {
              
        if(myGameThread == null) {
          myGameThread = new GameThread(myCanvas);
          myGameThread.start();
        } else {
          myGameThread.resumeGame();
        }
        
        if(myClownFishThread == null) {
          myClownFishThread = new ClownFishThread(myCanvas);
          myClownFishThread.start();
        } else {
          myClownFishThread.resumeGame();
        }
        
        if(myGoldFishThread == null)
        {
            myGoldFishThread = new GoldFishThread(myCanvas);
            myGoldFishThread.start();
        }
        else
            myGoldFishThread.resumeGame();
        
        if(myPiranhaThread == null)
        {
            myPiranhaThread = new PiranhaThread(myCanvas);
            myPiranhaThread.start();
        }
        else
            myPiranhaThread.resumeGame();
         
        
        if(musicPlayer == null) {
          musicPlayer = new MusicPlayer();
          musicPlayer.start();
        } else {
          musicPlayer.resumeGame();
        }
      }

      synchronized void userPauseThreads() {
        myGamePause = true;
        pauseThreads();
      }

      void systemPauseThreads() {
        myHiddenPause = true;
        pauseThreads();
      }

      private void pauseThreads() {
              
        if(myGameThread != null) {
          myGameThread.pauseGame();
        } 
        
        if(myClownFishThread != null) {
          myClownFishThread.pauseGame();
        } 
        
        if(myGoldFishThread != null)
        {
            myGoldFishThread.pauseGame();
        }
        
        if(myPiranhaThread != null)
        {
            myPiranhaThread.pauseGame();
        }
         
        if(musicPlayer != null) {
          musicPlayer.pauseGame();
        } 
      }

      private synchronized void stopThreads() {
        if(myGameThread != null) {
          myGameThread.requestStop();
        }
        
        if(myClownFishThread != null) myClownFishThread.requestStop();
        if(myGoldFishThread != null) myGoldFishThread.requestStop();
        if(myPiranhaThread != null) myPiranhaThread.requestStop();

        myGameThread = null;
        myClownFishThread = null;
        myGoldFishThread = null;
        myPiranhaThread = null;
        musicPlayer = null;
      }
}
